package com.lambkit.module.center.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.lambkit.common.util.PathUtils;
import com.lambkit.db.mgr.MgrTable;
import com.lambkit.module.meta.model.MetaImageSet;
import com.lambkit.module.meta.model.sql.MetaImageSetCriteria;
import com.lambkit.module.meta.service.MetaImageSetService;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.rpc.model.UpmsUser;
import com.lambkit.web.controller.LambkitController;

public class ImageSetController extends LambkitController {

	/**
	 * 获取某一folder的信息
	 */
	public void index() {
		MetaImageSetService service = MetaImageSet.service();
		if(service==null) {
			renderJson(Ret.fail().set("msg", "service is null"));
			return;
		}
		MgrTable tbc = getTableView(service.getTableName(), true);
		if (tbc == null) {
			renderJson(Ret.fail().set("msg", "table is not find."));
			return;
		}
		String model_id = getPara(0, getPara("id"));
		if (StrKit.notBlank(model_id)) {
			MetaImageSet model = service.findById(Long.valueOf(model_id));
			renderJson(Ret.ok().set("data", model));
		} else {
			renderJson(Ret.fail().set("msg", "id is null"));
		}
	}

	/**
	 * 获取folder列表
	 */
	public void list() {
		/*
		MetaStoreImageSetService service = MetaStoreImageSet.service();
		if(service==null) {
			renderJson(Ret.fail().set("msg", "service is null"));
			return;
		}
		MgrTable tbc = getTableView(service.getTableName(), true);
		if (tbc == null) {
			renderJson(Ret.fail().set("msg", "table is not find."));
			return;
		}
		List<Object> params = Lists.newArrayList();
		Long pid = getParaToLong(0,0L);
		SqlBuilder sb = new SqlBuilder();
		sb.appendSelect(tbc).appendFrom(tbc).appendIfNotEmpty("pid", pid, params, false);
		if(hasUser()) {
			sb.appendIfNotEmpty("user_id", Long.valueOf(getUser().getId().toString()), params, false);
		} else {
			sb.append(" and user_id is null ");
		}
		String sql =sb.build();
		List<Record> list = Db.find(sql, params);
		renderJson(Ret.ok().set("data", list));
		*/
		MetaImageSetService service = MetaImageSet.service();
		Long userId = null;
		MetaImageSetCriteria criteria = MetaImageSetCriteria.create();
		UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
		if(user!=null) {
			userId = user.getUserId();
			criteria.andUserIdEqualTo(userId);
		} else {
			criteria.andUserIdIsNull();
		}
		List<MetaImageSet> list = service.dao().findListByColumns(criteria.getList(), "orders,id", getParaToInt(0, 100));
		renderJson(Ret.ok().set("data", list));
	}
	
	public void save() {
		if("POST".equals(getRequest().getMethod())) {
			MetaImageSet imageSet = getModel(MetaImageSet.class, "model");
			if(StrKit.isBlank(imageSet.getName())) {
				renderJson(Ret.fail("msg", "Name is null or empty."));
				return;
			}
			imageSet.setCreated(new Date());
			UpmsUser user = getAttr("upmsUser") ==null ? UpmsManager.me().currentUser() : getAttr("upmsUser");
			if(user!=null) {
				Long userId = user.getUserId();
				imageSet.setUserId(userId);
			}
			boolean flag = imageSet.save();
			if(flag) {
				if(StrKit.isBlank(imageSet.getPath())) {
					imageSet.setPath(imageSet.getId().toString());
					if(imageSet.update()) {
						String path = new StringBuilder()
								.append(File.separator).append("attachment")
								.append(File.separator).append("imageset")
								.append(File.separator).append(imageSet.getPath()).toString();
						PathUtils.createDirectory(path);
					}
				}
				renderJson(Ret.ok());
			} else {
				renderJson(Ret.fail());
			}
		}
	}
	
	public void rename() {
		Long id = getParaToLong("id");
		if(id==null) {
			renderJson(Ret.fail().set("msg", "id is null"));
			return;
		}
		String title = getPara("title");
		if(StrKit.isBlank(title)) {
			renderJson(Ret.fail().set("msg", "title is null"));
			return;
		}
		MetaImageSet catalog = MetaImageSet.service().findById(id);
		catalog.setName(title);
		boolean flag = catalog.update();
		if(flag) {
			renderJson(Ret.ok());
		} else {
			renderJson(Ret.fail());
		}
	}
	
	/**
	 * 删除目录和file
	 */
	public void delete() {
		String ids = getPara(0);
		int n = MetaImageSet.service().deleteByPrimaryKeys(ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}
	
	/**
	 * 删除目录，不删除file
	 
	public void remove() {
		String ids = getPara(0);
		int n = MetaStoreImageSet.service().remove(ids);
		if(n > 0) {
			renderJson(Ret.ok().set("num", n));
		} else {
			renderJson(Ret.fail());
		}
	}
	*/
	
}
